<?php


/**
 * Description of model_bookmarks
 *
 * @author bilde
 */
class model_bookmarks extends CI_Model
{
    function  __construct()
    {
        parent::__construct();
    }

    //Add a bookmark to the 'bookmarks'-table
    function add_bookmark(){
		$userid = $this->session->userdata('id');
		$now = date("Y-m-d h:i:s");
		$data = array(
			'url' => xss_clean(substr($this->input->post('url'),0,255)),
			'description' => xss_clean(substr($this->input->post('description'),0,255)),
			'user_id' => $userid,
			'created' => $now
		);

		$this->db->insert("bookmarks",$data);		
		return $this->db->insert_id();

	}

        //Gets a bookmarks from 'bookmarks'-table by $id
	function get_bookmark($id){
		$this->db->where('id',$id);
		$this->db->limit(1);
		$Q = $this->db->get('bookmarks');
		if ($Q->num_rows() > 0){
			$data = $Q->row();
		}else{
			$data = array();
		}

		$Q->free_result();
		return $data;
	}

        //List all bookmarks for the 'logged_in_user'.
	function list_bookmarks(){
		$userid = $this->session->userdata('id');
		$data = array();
		$this->db->where('user_id',$userid);
		$this->db->order_by('created','asc');
		$Q = $this->db->get("bookmarks");
		if ($Q->num_rows() > 0){
			foreach ($Q->result() as $row){
				$data[$row->id] = $row;
			}
		}
		$Q->free_result();

		return $data;
	}

        //list bookmarks by user ($list_of_ids) and set a limit of 50.
	function list_user_bookmarks($list_of_ids,$limit=50)
        {           
            $data = array();           
            $this->db->select('id,url,created');
            $this->db->where_in("user_id",$list_of_ids);
            $this->db->order_by('created','asc');
            $this->db->limit($limit);
            $Q = $this->db->get("bookmarks");
            if ($Q->num_rows() > 0)
            {
                foreach ($Q->result() as $row)
                {
                    $data[$row->id] = $row;
                }
            }
            $Q->free_result();

            return $data;
	}

        //Gets a bookmarks from 'bookmarks'-table.
	function list_all_bookmarks()
        {
		$data = array();
		$this->db->order_by('created','asc');
		$Q = $this->db->get("bookmarks");
		if ($Q->num_rows() > 0)
                {
                    foreach ($Q->result() as $row)
                    {
                        $data[$row->id] = $row;
                    }
		}
		$Q->free_result();

		return $data;
        }


}
?>
